package org.vectrics.recruiter.struts.tag;

import java.util.List;
import java.util.Set;

import org.apache.ecs.html.A;
import org.apache.log4j.Logger;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.company.Company;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;

public class CandidateJobsTable extends BorderTable {

	private static final long serialVersionUID = -8541572360046988663L;
	
	private transient Logger log = Logger.getLogger(this.getClass());
	private Request request = null;
	private Person authPerson = null;
	//private String contextPath = null;
	
	public CandidateJobsTable(Person authPerson, Request request, List jobCandidates) {
		super();
		this.setWidth("100%");
		this.request = request;
		this.authPerson = authPerson;
		//this.contextPath = contextPath;
		render(jobCandidates);
	}

	private String getPhrase(String code) {
		return CoreServiceLocator.getPhraseProvider().getMessage(code, request.getLocale());
	}
	
	
	private void render(List jobCandidates) {
		Row row = this.addRow();
        row.setDefaultBgImage(this.request.getContextPath() + "/site-graphics/tbl_hdr.gif");
		Cell cell = row.addCell();
		cell.setVAlign("top");
		
		Set authDomains = CoreServiceLocator.getPersonService().findDomainsThatPersonCanAccess(this.authPerson);
		Set authCompanies = CoreServiceLocator.getCompanyService().findCompaniesForDomains(authDomains);
		if (authCompanies.size() > 0) {
			cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.company")));
			cell = row.addCell();
			cell.setVAlign("top");
		}
	
		cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.job.title")));
		
		cell = row.addCell();
		cell.setVAlign("top");
		cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("prompt.job.candidate.status")));
		
		cell = row.addCell();
		cell.setVAlign("top");
	
		if (jobCandidates.size() == 0) {
			int columnCount = row.getColumnCount();
			row = this.addRow();
			cell = row.addCell();
			cell.setVAlign("top");
			cell.setColSpan(columnCount);
			cell.addText("stdFont", this.getPhrase("phrase.no.jobs.for.candidate"));
			
		} else {
			for (int i = 0; i < jobCandidates.size(); i++) {
				JobCandidate jobCandidate = (JobCandidate)jobCandidates.get(i);
				Job job = jobCandidate.getJob();
				Domain domain = job.getCompany().getDomain();
				if (authPerson.hasDomainOrGlobalRole("job.view.candidates", domain)) {
					row = this.addRow();
					if (authCompanies.size() > 0) {
						cell = row.addCell();
						cell.setVAlign("top");
						cell.addElement(TextHelper.getFont("subFont", this.getCompanyName(job)));
					}
					
					cell = row.addCell();
					cell.setVAlign("top");
					cell.addElement(TextHelper.getFont("subFont", job.getTitle()));
					
					cell = row.addCell();
					cell.setVAlign("top");
					if (jobCandidate.getCurrentStatus() != null) {
						cell.addElement(TextHelper.getFont("subFont", 
								jobCandidate.getCurrentStatus().getStatusType().getDescription()));
					}
						
					cell = row.addCell();
					cell.setVAlign("top");
					A jobLink = new A();
					jobLink.setClass("subLink");
					jobLink.setHref(request.getContextPath() + "/admin/jobs/view.do?jobId=" + job.getId());
					jobLink.setOnClick("OpenLarge(this.href, '" + job.getId() + "'); return false");
					jobLink.addElement("job details");
					cell.addElement(jobLink);
					cell.addElement("&nbsp;");
					
					A jobCandidateLink = new A();
					jobCandidateLink.setClass("subLink");
					jobCandidateLink.setHref(this.getJobCandidateHref(jobCandidate));
					jobCandidateLink.setOnClick("OpenLarge(this.href, '" + job.getId() + "'); return false");
					jobCandidateLink.addElement("candidate info");
					cell.addElement(jobCandidateLink);
				}				
			}
		}
	}
	
    private String getJobCandidateHref(JobCandidate jobCandidate) {
    	String href = null;                      
    	href = request.getContextPath() + "/admin/jobs/candidate/view/index.do?jobId="
    		+ jobCandidate.getJob().getId().toString()
    		+ "&userId="  
    		+ jobCandidate.getCandidate().getPerson().getId().toString() 
    		+ "&jobCandidateId=" + jobCandidate.getId();
    	return(href);
    }
    		
	
    private String getCompanyName(Job job) {
    	String companyName = "";
    	try {
	    	if (job.getCompany() != null) {
	    		Company company = job.getCompany();
	    		if (company != null) {
	    			companyName = company.getName();
	    		}
	    	}
    	}
    	catch (Exception ex) {
    		log.error("Exception finding company", ex);
    	}
    	return(companyName);
    }
	
    
}
